<template>
  <div id="add">
    <el-card :body-style="{ padding: '0px' }" shadow="nerver">
      <div slot="header">
        <span>编辑用户</span>
        <el-button
          type="info"
          size="mini"
          plain
          icon="el-icon-back"
          @click="$router.push({name:'users'})"
        >返回</el-button>
      </div>
      <!-- card body -->
      <el-form :model="form" ref="form" :rules="rules" label-width="80px" style="margin-top:15px" v-loading="loading">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.gender">
            <el-option label="男" value="m"></el-option>
            <el-option label="女" value="f"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="edit('form')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  props: ["id"],
  data() {
          let validateusername = (rule, value, callback) => {
      this.axios
          .get(`/users/exists/username/${value}`, {
            field:"username",
            value: value
          })
          .then((ret) => {
            console.log(ret.data)
           if(ret.data){
              callback(new Error('用户名已被注册'))
           }else{
             callback()
           }
          });
    };
        let validatephone = (rule, value, callback) => {
      this.axios
          .get(`/users/exists/phone/${value}`, {
            field:"phone",
            value: value
          })
          .then((ret) => {
            console.log(ret.data)
           if(ret.data){
              callback(new Error('此号码被注册使用'))
           }else{
             callback()
           }
          });
    };
     let validateemail = (rule, value, callback) => {
      this.axios
          .get(`/users/exists/email/${value}`, {
            field:"email",
            value: value
          })
          .then((ret) => {
           if(ret.data){
              callback(new Error('此邮箱被使用'))
           }else{
             callback()
           }
          });
    };
    return {
      loading:false,
      form: {
        username: "",
        phone: "",
        email: "",
        gender: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 4,
            max: 18,
            message: "长度在 4 到 18 个字符",
            trigger: "blur"
          },
          {validator: validateusername,trigger: 'blur'}
        ],
        phone: [
          { required: true, message: "请输入11位的手机号", trigger: "blur" },
          { type: "number", message: "请输入11位的手机号", trigger: "blur" },
          {
            pattern: /^1[3|4|5|7|8][0-9]\d{8}$/,
            message: "手机格式不对",
            trigger: "blur"
          },
            {validator: validatephone,trigger: 'blur'}
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur"]
          },
           {validator: validateemail,trigger: 'blur'}
        ],
        gender: [{ required: true, message: "请选择性别", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getuser();
  },
  methods: {
    getuser() {
      this.loading=true;
      this.axios
        .get(`/users/${this.id}`)
        .then(ret => {
          this.loading=false
          let { data, error } = ret.data;
          this.form = data;
        })
        .catch();
    },
    edit(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          this.axios
            .post(`/users/edit/${this.id}`, this.form)
            .then(() => {
              this.$router.push({
                name: "users"
              });
            })
            .catch(() => {
              alert("提交失败");
            });
        }
      });
    }
  }
};
</script>

<style>
</style>